﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.BillPlugIn.BPW
{
    [Description("BPW:票面字段规则开发需求-普票"), HotUpdate]
    public class SaleSicInforeBp : AbstractBillPlugIn
    {
        public override void BeforeSave(BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);
            List<DynamicObject> list = new List<DynamicObject>();
            var obj = this.Model.DataObject;//SALESICENTRY
            DynamicObjectCollection rows = obj["SALESICENTRY"] as DynamicObjectCollection;
            for (int i = 0; i < rows.Count; i++)
            {
                //F_QHAT_Base_dvn 源出库单单据类型
                //F_BPW_KPNAME 开票名称  F_BPW_KPNAME
                //F_BPW_KPModle  开票规格型号 F_BPW_KPMODLE T_IV_SALESICENTRY T_IV_SALESICENTRY
                //F_BPW_KPQTY 开票数量
                //F_BPW_KPUnitID 开票单位 F_BPW_KPUNITID1
                //F_BPW_KPPrice 开票单价
                var mat = rows[i]["MATERIALID"] as DynamicObject;
                string kpmc = mat["F_BPW_OUTNAME"].ToString();//物料上的出口报关名称
                string th = mat["F_BPW_Dwgnumber"].ToString();//图号
                var kpdw = mat["F_BPW_OUTUnitID"] as DynamicObject;//报关单位
                string dwNumber = mat["F_BPW_OUTUnitID"] == null ? "" : kpdw["number"].ToString();
                //string wlfz = mat["FMaterialGroup"].ToString();//物料分组
                //string wlName = mat["name"].ToString();//物料名称
                //string wlNumber = mat["number"].ToString();//物料编码
                //string rhzdw = mat["F_BPW_RHZUnitID"].ToString();//润滑脂单位
                //string chlb = mat["FCategoryID"].ToString();//存货类别
                //string czcs = mat["F_BPW_CZparameter"].ToString();//车轴参数
                //string jbdw = mat["FBaseUnitId"].ToString();//基本单位

                decimal jjsl = Convert.ToDecimal(rows[i]["PRICEQTY"]);//计价数量
                decimal jshj = Convert.ToDecimal(rows[i]["ALLAMOUNTFOR"]);//价税合计
                //var dvn = rows[i]["F_QHAT_Base_dvn"] as DynamicObject;//源出库单单据类型
                //list.Add(dvn);
                //string dvname = dvn["name"].ToString();
                this.Model.SetValue("F_BPW_KPNAME", kpmc , i);
                this.Model.SetValue("F_BPW_KPModle", th, i);
                this.Model.SetValue("F_BPW_KPQTY", jjsl, i);
                //this.Model.SetValue("F_BPW_KPUNITID1", kpdw, i);
                this.Model.SetItemValueByNumber("F_BPW_KPUNITID1", dwNumber, i);
                this.Model.SetValue("F_BPW_KPPrice", jshj / jjsl, i);
            }
            //if (list.Distinct().Count() > 1)
            //{
            //    this.View.ShowErrMessage("不允许有多个源出库单单据类型！");
            //    e.Cancel = true;
            //}
        }
    }
}
